//
//  HomeSearchViewController.swift
//  Community
//
//  Created by JKSD on 2019/8/5.
//  Copyright © 2019 ZKJ. All rights reserved.
//

import UIKit
import AVFoundation
import SwiftyJSON

class SWPTHomeSearchViewController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
    let search = UITextField()

    override func viewWillAppear(_ animated: Bool) {
        super .viewWillAppear(animated)
        
        self.navigationItem.hidesBackButton = true
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

//        self.title = "分类查询"
        self.fd_prefersNavigationBarHidden = true
        self.view.backgroundColor = UIColor.white
        let banner = UIImageView.init(frame: CGRect (x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_WIDTH*2/3.0))
        banner.image = UIImage.init(named: "kaka.jpg")
        banner.contentMode = .scaleAspectFill
        
        self.view.addSubview(banner)
        
        
        search.frame =  CGRect (x: 20, y: banner.n_maxY + 40, width: SCREEN_WIDTH - 40, height: 50)
        search.borderStyle = UITextField.BorderStyle.roundedRect
        search.textAlignment = NSTextAlignment.center
        search.placeholder = "输入要查询的内容".international()
        self.view.addSubview(search)
        
        let sb = UIButton.init(type: UIButton.ButtonType.custom)
        sb.frame = CGRect (x: 20, y: search.n_maxY + 25, width: SCREEN_WIDTH - 40, height: 40)
        sb.layer.cornerRadius = 20
        sb.clipsToBounds = true
        sb.backgroundColor = mainGreenColor //UIColor.init(r: 51/255.0, g: 150/255/0, b: 251/255.0, alpha: 1)
        sb.setTitle("查询".international(), for: UIControl.State.normal)
        sb.setTitleColor(UIColor.white, for: UIControl.State.normal)
        sb.titleLabel?.font = constant.kFontSize(x: 18)
        self.view.addSubview(sb)
        sb.addTarget(self, action: #selector(tosearch), for: UIControl.Event.touchUpInside)
        
        
        let camre = UIButton.init(type: .custom)
        camre.frame = CGRect (x: (SCREEN_WIDTH - 73)/2.0, y: sb.n_maxY + 40, width: 71, height: 71)
        camre.setImage(UIImage.init(named: "aperture_camera_focus_lens_219.17391304348px_1211610_easyicon.net")!.imageBy(tintColor: mainGreenColor), for: UIControl.State.normal)
        camre.imageView?.contentMode = .scaleAspectFit
        camre.addTarget(self, action: #selector(goCamera), for: UIControl.Event.touchUpInside)
        camre.layer.cornerRadius = 35.5
        camre.layer.borderWidth = 1
        camre.layer.borderColor = mainGreenColor.cgColor
        camre.layer.masksToBounds = true
        self.view.addSubview(camre)
        
        let tip = UILabel.init(frame: CGRect (x: 15, y: StatusBarHeight + 20, width: SCREEN_WIDTH - 30, height: SCREEN_HEIGHT - AllTopNavHeight - camre.n_maxY - TabBarHeight))
        tip.numberOfLines = 0
        tip.font = constant.kFontSize(x: 14)
        tip.textColor = UIColor.white
        tip.text = "    认清四色垃圾桶，垃圾分类要清楚，蓝色大桶可回收，变废为宝好颜色，绿色大桶装剩菜，变成肥料来灌溉，红桶垃圾最有害，电池灯管就是他，橘色大桶装其他，尿布旧物来安家。"
        let shadow = NSShadow.init()
        shadow.shadowBlurRadius = 10.0
        shadow.shadowOffset = CGSize(width: 5, height: 5)
        shadow.shadowColor = UIColor.gray
        tip.attributedText = NSAttributedString.init(string: tip.text!, attributes: [NSAttributedString.Key.shadow : shadow])
        self.view.addSubview(tip)
        
    }
    @objc func backAct() {
        self.navigationController?.popViewController(animated: true)
    }
    
   @objc func tosearch() {
        if self.search.text?.count == 0 {
            return
        }
        let ok = SWPTOKViewController()
        ok.searchKey(keyWord: self.search.text!)
        self.navigationController?.pushViewController(ok, animated: true)
    }
    
    @objc func tosearchImg(img:UIImage){
        let ok = SWPTOK2ViewController()
        ok.searchImgy(img: img)
        self.navigationController?.pushViewController(ok, animated: true)
    
    }
    
    
  @objc  func goCamera() {
    
        //先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
        var sourceType = UIImagePickerController.SourceType.camera
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
            sourceType = UIImagePickerController.SourceType.photoLibrary
        }
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.allowsEditing = true//设置可编辑
        picker.sourceType = sourceType
        self.present(picker, animated: true, completion: nil)//进入照相界面
    }
    
    func goImage(){
        let pickerImage = UIImagePickerController()
        
        if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            
            pickerImage.sourceType = UIImagePickerController.SourceType.photoLibrary
            pickerImage.mediaTypes = UIImagePickerController.availableMediaTypes(for: pickerImage.sourceType)!
        }
        pickerImage.delegate = self
        pickerImage.allowsEditing = true
        self.present(pickerImage, animated: true, completion: nil)
    }
    
    //选择好照片后choose后执行的方法
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
        let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
        self.tosearchImg(img: image)
        picker.dismiss(animated: true, completion: nil)
    }
    
    //cancel后执行的方法
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
        
    }
    
    
}
